import { emojiImg } from "@/services/local-data";
import axios from "../services/axios";

export function getCount(count) {
  if (count < 0) return count;
  if (count < 10000) return count;
  if (Math.floor(count / 10000) < 10000) {
    return Math.floor(Math.floor(count / 1000) / 10) + "万";
  } else {
    return Math.floor(Math.floor(count / 10000000) / 10) + "亿";
  }
}

export function getCountPoint(count) {
  if (count < 0) return count;
  if (count < 10000) return count;
  if (Math.floor(count / 10000) < 10000) {
    return Math.floor(count / 1000) / 10 + "万";
  } else {
    return Math.floor(count / 10000000) / 10 + "亿";
  }
}

export function getMonthDay(time) {
  let getDate = new Date(time);
  let newDatas = getDate.getMonth() + 1 + "月" + getDate.getDate() + "日";
  return newDatas;
}
export function getYearMonthDay(time) {
  let getDate = new Date(time);
  let Y = getDate.getFullYear() + "-";
  let M =
    (getDate.getMonth() + 1 < 10
      ? "0" + (getDate.getMonth() + 1)
      : getDate.getMonth() + 1) + "-";
  let D = getDate.getDate() < 10 ? "0" + getDate.getDate() : getDate.getDate();
  let h = getDate.getHours() + ":";
  let m = getDate.getMinutes() + ":";
  let s = getDate.getSeconds();
  return Y + M + D;
}
export function getMonthDayHouMin(time) {
  let getDate = new Date(time);
  let M = getDate.getMonth() + 1;
  let D = getDate.getDate() < 10 ? "0" + getDate.getDate() : getDate.getDate();
  let h =
    getDate.getHours() < 10
      ? "0" + getDate.getHours() + ":"
      : getDate.getHours() + ":";
  let m =
    getDate.getMinutes() < 10
      ? "0" + getDate.getMinutes()
      : getDate.getMinutes();
  return M + "月" + D + "日  " + h + m;
}

export function getSizeImage(imgUrl, size) {
  return `${imgUrl}?param=${size}x${size}`;
}
export function getSizeImageXY(imgUrl, sizeX, sizeY) {
  return `${imgUrl}?param=${sizeX}y${sizeY}`;
}
export function getSizeImageSquare(imgUrl, sizeX, sizeY) {
  return `${imgUrl}?param=${sizeX}x${sizeY}`;
}

export function getPlayUrl(id) {
  return axios({
    url: "/song/url",
    params: {
      id,
    },
  });
  // return `/api/song/media/outer/url?id=${id}.mp3`
}

export function getfg(itme, item, indexa, id) {
  let newStr = "";
  let hbNewStr = "";
  if (item.ar.length == 1) {
    return (newStr = itme);
  } else {
    if (indexa == 0) {
      return (newStr = itme);
    } else {
      return (newStr = "/" + itme);
    }
  }
}
export function formalMinuteSeconds(currentTime) {
  let minute = Math.floor(currentTime / 1000 / 60) + "";
  let second =
    Math.floor(
      Math.floor(currentTime - Math.floor(currentTime / 1000 / 60) * 60000) /
        1000
    ) + "";

  if (minute < 10) {
    minute = "0" + minute;
  }
  if (second < 10) {
    second = "0" + second;
  }

  return minute + ":" + second;
}

export function formalMinuteSecondsF(currentTime) {
  let minute = Math.floor(currentTime / 1000 / 60) + "";
  let second =
    Math.floor(
      Math.floor(currentTime - Math.floor(currentTime / 1000 / 60) * 60000) /
        1000
    ) + "";

  if (minute < 10) {
    minute = "0" + minute;
  }
  if (second < 10) {
    second = "0" + second;
  }

  return minute + "分" + second + "秒";
}

export function getMinSecondMsecond(time) {
  if (time) {
    let s = "";
    let min = time.split(":")[0];
    let sec = time.split(":")[1].split(".")[0];
    let msec = time.split(":")[1].split(".")[1];
    s = Number(min * 60000) + Number(sec * 1000) + Number(msec);
    //    console.log(s)
    return s;
  }
}

export function getMusicId(id) {
  if (!window.localStorage.getItem("ids")) {
    let arrId = [];
    arrId.push(id);
    // console.log(arrId)
    window.localStorage.setItem("ids", JSON.stringify(arrId));
  } else {
    const arr = JSON.parse(window.localStorage.getItem("ids")).map(
      (item, index) => {
        return item;
      }
    );
    if (arr.indexOf(id) === -1) {
      arr.push(id);
      window.localStorage.setItem("ids", JSON.stringify(arr));
    }
  }
}

export function contentEmoji(content) {
  var regex = /\[+\S+?\]/g;
  if (content.match(regex) != null) {
    return content.replaceAll(regex, () => {
      return content.match(regex).map((item, index) => {
        return emojiImg
          .filter((itema) => itema.title === item.split("[")[1].split("]")[0])
          .map((itemb, indexb) => {
            return "_!" + itemb.url;
          });
      });
    });
  } else {
    return content;
  }
}
